package vip.xiaonuo.supplier.modular.custom.customtype.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.supplier.modular.custom.customtype.entity.SupplierCustomType;
import vip.xiaonuo.supplier.modular.custom.customtype.mapper.SupplierCustomTypeMapper;
import vip.xiaonuo.supplier.modular.custom.customtype.param.SupplierCustomTypeAddParam;
import vip.xiaonuo.supplier.modular.custom.customtype.param.SupplierCustomTypeEditParam;
import vip.xiaonuo.supplier.modular.custom.customtype.param.SupplierCustomTypeIdParam;
import vip.xiaonuo.supplier.modular.custom.customtype.param.SupplierCustomTypePageParam;
import vip.xiaonuo.supplier.modular.custom.customtype.service.SupplierCustomTypeService;

import java.util.List;

/**
 * 客户类型Service接口实现类
 *
 * @author Mr.wu
 * @date  2025/03/15 17:38
 **/
@Service
public class SupplierCustomTypeServiceImpl extends ServiceImpl<SupplierCustomTypeMapper, SupplierCustomType> implements SupplierCustomTypeService {

    @Override
    public Page<SupplierCustomType> page(SupplierCustomTypePageParam supplierCustomTypePageParam) {
        QueryWrapper<SupplierCustomType> queryWrapper = new QueryWrapper<SupplierCustomType>().checkSqlInjection();
        if(ObjectUtil.isNotEmpty(supplierCustomTypePageParam.getCustomTypeName())) {
            queryWrapper.lambda().eq(SupplierCustomType::getCustomTypeName, supplierCustomTypePageParam.getCustomTypeName());
        }
        if(ObjectUtil.isNotEmpty(supplierCustomTypePageParam.getCustomTypeStatus())) {
            queryWrapper.lambda().eq(SupplierCustomType::getCustomTypeStatus, supplierCustomTypePageParam.getCustomTypeStatus());
        }
        //数据归属
        queryWrapper.lambda().eq(SupplierCustomType::getCompanyId, StpLoginUserUtil.getLoginUser().getCompanyId());
        if(ObjectUtil.isAllNotEmpty(supplierCustomTypePageParam.getSortField(), supplierCustomTypePageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(supplierCustomTypePageParam.getSortOrder());
            queryWrapper.orderBy(true, supplierCustomTypePageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(supplierCustomTypePageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(SupplierCustomType::getSortCode);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SupplierCustomTypeAddParam supplierCustomTypeAddParam) {
        SupplierCustomType supplierCustomType = BeanUtil.toBean(supplierCustomTypeAddParam, SupplierCustomType.class);
        //数据归属
        supplierCustomType.setCompanyId(StpLoginUserUtil.getLoginUser().getCompanyId());
        this.save(supplierCustomType);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SupplierCustomTypeEditParam supplierCustomTypeEditParam) {
        SupplierCustomType supplierCustomType = this.queryEntity(supplierCustomTypeEditParam.getId());
        BeanUtil.copyProperties(supplierCustomTypeEditParam, supplierCustomType);
        this.updateById(supplierCustomType);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<SupplierCustomTypeIdParam> supplierCustomTypeIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(supplierCustomTypeIdParamList, SupplierCustomTypeIdParam::getId));
    }

    @Override
    public SupplierCustomType detail(SupplierCustomTypeIdParam supplierCustomTypeIdParam) {
        return this.queryEntity(supplierCustomTypeIdParam.getId());
    }

    @Override
    public SupplierCustomType queryEntity(String id) {
        SupplierCustomType supplierCustomType = this.getById(id);
        if(ObjectUtil.isEmpty(supplierCustomType)) {
            throw new CommonException("客户类型不存在，id值为：{}", id);
        }
        return supplierCustomType;
    }

    @Override
    public List<SupplierCustomType> customTypelist(){
        QueryWrapper<SupplierCustomType> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("ID","CUSTOM_TYPE_NAME");
        return this.list(queryWrapper.lambda().eq(SupplierCustomType::getCompanyId,StpLoginUserUtil.getLoginUser().getCompanyId()));
    }
}
